package com.spring.study.configuration;

import com.spring.study.service.DemoService;
import com.spring.study.service.DemoServiceProperties;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(DemoService.class)
@EnableConfigurationProperties(DemoServiceProperties.class)
public class DemoAutoConfiguration {

    @Autowired
    private DemoServiceProperties properties;

    /**
     * 当属性中有enable并且为true的时候初始化这个Bean
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "demo.service.enable",havingValue = "true",matchIfMissing = true)
    public DemoService demoService1(){

        return new DemoService(properties.getPrefix(), properties.getSuffix());
    }

    /**
     * 当缺失这个Bean的时候创建
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public DemoService demoService(){

        return new DemoService("候选" +properties.getPrefix(), properties.getSuffix());
    }

}
